<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 * @package    ZNF
 * @subpackage Util
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */

/**
 * <i>ZNF_Util_Utility</i> class provides useful methods.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Util
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Util_Utility
{

    /**
     * Private constructor to avoid <i>ZNF_Util_Utility</i> object creation.
     *
     * @access private
     */
    private function __construct()
    {
    }

    /**
     * Private clonator to avoid <i>ZNF_Util_Utility</i> object clonation.
     *
     * @access private
     */
    private function __clone()
    {
    }

    /**
     * Exports a generic array in a well formed XML string.
     *
     * @static
     * @access public
     * @param array $array
     * @param int $level Level of iteration, determines the depth of the
     *                   array currently processed
     * @param string $version
     * @param string $encoding
     * @return string
     */
    static public function arrayToXML($array, $level = 0, $version = '1.0', $encoding = 'utf-8')
    {
        if ($level == 0) {
            $xml = "<?xml version=\"$version\" encoding=\"$encoding\" ?>\n";
            $xml .= "<content>\n";
        }

        $result = null;

        // calculate spaces for indentation
        $indent = '  ';

        for ($i = 0; $i<$level; $i++) {
            $indent .= '  ';
        }

        // process the array
        foreach ($array as $name => $value) {

            if (is_array($value) && is_int($name)) {
                $result .= "$indent<item key=\"$name\">\n";
                $result .= self::arrayToXML($value, $level+1);
                $result .= "$indent</item>\n";
            } elseif (is_array($value)) {
                $result .= "$indent<$name>\n";
                $result .= self::arrayToXML($value, $level+1);
                $result .= "$indent</$name>\n";
            } elseif (is_int($name) && $value != null) {
                $result .= "$indent<item key=\"$name\">" . htmlspecialchars($value) . "</item>\n";
            } elseif (is_int($name)) {
                $result .= "$indent<item key=\"$name\"/>\n";
            } elseif ($value != null) {
                $result .= "$indent<$name>" . htmlspecialchars($value) . "</$name>\n";
            } else {
                $result .= "$indent<$name/>\n";
            }

        }

        if ($level == 0) {
            $xml .= $result;
            $xml .= "</content>\n";
            return $xml;
        } else {
            return $result;
        }
    }

    /**
     * Checks if an email address is valid.
     *
     * @static
     * @access public
     * @param string $email
     * @return boolean
     */
    static public function checkEmail($email)
    {
        $mxhosts = null;

        if (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email)) {
            return false;
        }

        list ($username, $domain) = split ('@', $email);

        if (checkdnsrr($domain, 'MX'))  {

            if (!getmxrr($domain, $mxhosts)) {
                return false;
            }

            $connectAddress = $mxhosts[0];
        } else {
            return false;
        }

        $connect = fsockopen($connectAddress, 25);

        if (!$connect) {
            return false;
        }

        $out = fgets($connect, 1024);

        if (ereg("^220", $out)) {
            fputs ($connect, "HELO 82.49.142.198\r\n");
            $out = fgets($connect, 1024);
            fputs ($connect, "MAIL FROM: <" . $email . ">\r\n");
            $from = fgets($connect, 1024);
            fputs ($connect, "RCPT TO: <" . $email . ">\r\n");
            $to = fgets($connect, 1024);
            fputs($connect, "QUIT\r\n");
            fclose($connect);

            if (!ereg ("^250", $from ) || !ereg ( "^250", $to )) {
                return false;
            }

        }

        return true;
    }

    /**
     * Checks if an URL is valid and returns it in the correct format.
     *
     * @static
     * @access public
     * @param string $url
     * @return string
     */
    static public function checkUrl($url)
    {
        $mxhosts = null;
        $domain = preg_replace('/http:\/\/|https:\/\/|http:\/\/www.|https:\/\/www.|^www./', '', $url);
        $parts = explode('/', $domain);
        $domain = $parts[0];

        if ((getmxrr($domain, $mxhosts) == FALSE && gethostbyname($domain) == $domain)) {
            return false;
        }

        if (!(strstr($url, 'http://') || strstr("Url $url not valid", 'https://'))) {
            $url = "http://$url";
        }

        return $url;
    }

    /**
     * Checks a date and returns is timestamp.
     *
     * @static
     * @access public
     * @param string $year
     * @param string $month
     * @param string $day
     * @return timestamp
     */
    static public function getDateTimestamp($year = null, $month = null, $day = null)
    {
        if (!(checkdate($month, $day, $year))) {
            $translation = new ZNF_Presentation_Translation('ZNF', $_SESSION['znf']['lang']);
            throw new ZNF_Util_UtilityException($translation->get('errorDateNotValid') . ": $day-$month-$year");
        }

        return mktime(0, 0, 0, $month, $day, $year);
    }

    /**
     * Applies addslaslashes PHP function to all the elements of an array.
     *
     * @static
     * @access public
     * @param array &$array
     * @return array
     */
    static public function &addAllSlashes(&$array)
    {
        foreach ($array as &$current) {

            if (is_array($current)) {
                $current = self::addAllSlashes($current);
            } else {
                $current = addslashes($current);
            }

        }

        return $array;
    }

    /**
     * Applies stripslaslashes PHP function to all the elements of an array.
     *
     * @static
     * @access public
     * @param array &$array
     * @return array
     */
    static public function &stripAllSlashes(&$array)
    {
        foreach ($array as &$current) {

            if (is_array($current)) {
                $current = self::stripAllSlashes($current);
            } else {
                $current = stripslashes($current);
            }

        }

        return $array;
    }

    /**
     * Destroys the <i>ZNF_Util_Utility</i> object.</>
     *
     * @access public
     */
    public function __destruct()
    {
    }

}

/**
 * <i>ZNF_Util_UtilityException</i> is the exception type for the <i>ZNF_Util_Utility</i> class.
 *
 * <i>ZNF_Util_UtilityException</i> extends the <i>Exception</i> class of PHP5.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Util
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Utility.php 44 2007-07-22 23:55:30Z evolic $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Util_UtilityException extends Exception
{
}

?>
